package com.zzl.alligator.framework.support.web.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zzl.alligator.management.system.domain.SysDept;
import com.zzl.alligator.management.system.domain.SysMenu;
import com.zzl.alligator.management.system.domain.SysUser;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构实体类
 *
 * @author zhouziliang
 * @since 2020/06/26
 */
public class TreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private Long id;

    /**
     * 唯一节点 id+label
     */
    private String nodeId;

    /**
     * 节点名称
     */
    private String label;

    /**
     * 是否叶子节点
     */
    private boolean leaf;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelect> children;

    public TreeSelect() {
    }

    public TreeSelect(SysDept dept) {
        this.id = dept.getDeptId();
        this.label = dept.getDeptName();
        this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(SysMenu menu) {
        this.id = menu.getMenuId();
        this.label = menu.getMenuName();
        this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(SysUser user) {
        this.id = user.getUserId();
        this.label = user.getNickName();
        this.leaf = true;
        this.nodeId = "user-" + user.getUserId();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<TreeSelect> getChildren() {
        return children;
    }

    public void setChildren(List<TreeSelect> children) {
        this.children = children;
    }

    public boolean isLeaf() {
        return leaf;
    }

    public void setLeaf(boolean leaf) {
        this.leaf = leaf;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public void addChild(TreeSelect treeSelect) {
        if(this.children == null) {
            this.children = new ArrayList<>();
        }
        this.children.add(treeSelect);
    }
}
